﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMSBusiness
{
    public class PermissionBusiness
    {
        /// <summary>
        /// 1.判断 当前登录用户的buttonevent 权限  User Permission 表
        /// 2.判断当前report 的状态的 button event 权限 ReportStatus To Button Event表
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="buttonEventID"></param>
        /// <param name="reportHeaderID"></param>
        /// <returns></returns>
        public static void CheckButtonVisible(int userID, int reportHeaderID, ref bool boolAdd, ref bool boolChange, ref bool boolConfirm, ref bool boolExport, ref bool boolPrint, ref bool boolRemove)
        {

            var reportHeader = new ReportHeaderEntity(reportHeaderID);
            if (!reportHeader.IsNew)
            {
                var boolAddUser = false;
                var boolChangeUser = false;
                var boolConfirmUser = false;
                var boolExportUser = false;
                var boolPrintUser = false;
                var boolRemoveUser = false;

                var boolAddReport = false;
                var boolChangeReport = false;
                var boolConfirmReport = false;
                var boolExportReport = false;
                var boolPrintReport = false;
                var boolRemoveReport = false;

                //step 1 check user permission
                var userPerEntity = new UserPermissionEntity(userID, reportHeader.ReportCategoryId);
                if (!userPerEntity.IsNew)
                {
                    boolAddUser = userPerEntity.Added;
                    boolChangeUser = userPerEntity.Changes;
                    boolConfirmUser = userPerEntity.ToConfirm;
                    boolExportUser = userPerEntity.Export;
                    boolPrintUser = userPerEntity.Printing;
                    boolRemoveUser = userPerEntity.Remove;
                }



                var reportToEventCollection = new ReportStatusToButtonEventCollection();
                reportToEventCollection.GetMulti(ReportStatusToButtonEventFields.ReportStatusId ==
                                                      reportHeader.Status);
                foreach (var reportToEventEntity in reportToEventCollection)
                {
                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Add)
                    {
                        boolAddReport = true;

                    }

                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Change)
                    {

                        boolChangeReport = true;
                    }
                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Confirm)
                    {

                        boolConfirmReport = true;

                    }
                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Export)
                    {

                        boolExportReport = true;

                    }
                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Print)
                    {

                        boolPrintReport = true;

                    }
                    if (reportToEventEntity.ButtonEventId == DictionaryCategoryConst.ButtonPermission.Remove)
                    {

                        boolRemoveReport = true;
                    }

                }

                boolAdd = boolAddUser&& boolAddReport;
                boolConfirm = boolConfirmUser && boolConfirmReport;
                boolChange = boolChangeUser && boolChangeReport;
                boolExport = boolExportUser && boolExportReport;
                boolPrint = boolPrintUser && boolPrintReport;
                boolRemove = boolRemoveUser && boolRemoveReport;


            }
           
        }


        //public static bool CheckButtonVisible(int userID, int buttonEventID, int reportHeaderID)
        //{

        //    var reportHeader = new ReportHeaderEntity(reportHeaderID);
        //    if (!reportHeader.IsNew)
        //    {
        //        var boolHasPermission = true;
        //        if (boolHasPermission)
        //        {
        //            var userPerEntity = new UserPermissionEntity(userID, reportHeader.ReportCategoryId);
        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Add)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.Added;
        //            }

        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Change)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.Changes;
        //            }

        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Confirm)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.ToConfirm;
        //            }

        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Export)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.Export;
        //            }

        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Print)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.Printing;
        //            }
        //            if (buttonEventID == DictionaryCategoryConst.ButtonPermission.Remove)  //判断是哪个event,
        //            {
        //                boolHasPermission = userPerEntity.Remove;
        //            }
        //        }

        //        if (boolHasPermission)  //check report 状态 与event 关系
        //        {

        //            var reportToEventCollection = new ReportStatusToButtonEventCollection();
        //            var intCount = reportToEventCollection.GetDbCount(ReportStatusToButtonEventFields.ReportStatusId ==
        //                                                  reportHeader.Status &
        //                                                  ReportStatusToButtonEventFields.ButtonEventId == buttonEventID);
        //            boolHasPermission = intCount > 0;
        //        }
        //        return boolHasPermission;
        //    }
        //    return false;
        //}
    }
}
